package com.xdl.flytest.controller;


import com.xdl.flytest.bean.SystemUser;
import com.xdl.flytest.service.UserService;
import com.xdl.flytest.util.JsonResponseMessage;
import com.xdl.flytest.util.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 80736
 * @Date: 2018/12/25 11:48
 * @Description:
 */

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/reg", method = RequestMethod.GET)
    public String regPage() {
        return "user/page-register";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String registerUser(@Validated SystemUser user, BindingResult bindingResult, HttpSession session) {
        if (bindingResult.hasErrors()) {
            return "redirect:user/reg";
        }

        if (user != null) {
            userService.addUser(user);
            session.setAttribute("email", user.getEmail());
            session.setAttribute("nickname", user.getNickName());
            return "redirect:/";
        }

        return "redirect:user/reg";
    }


    @RequestMapping(value = "/userlogin", method = {RequestMethod.POST})
    public String userlogin(SystemUser user, HttpSession session) {
        // todo 用户密码加密解密
        if (user.getEmail() != null && user.getPassword() != null) {
            SystemUser systemUser = userService.getUserByEmail(user.getEmail());
            if (systemUser != null && systemUser.getPassword().equals(user.getPassword())) {
                session.setAttribute("email", systemUser.getEmail());
                session.setAttribute("nickname", systemUser.getNickName());
                return "redirect:/";
            }
        }
        return "user/page-login";
    }


    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginPage() {
        return "user/page-login";
    }


    @RequestMapping("/logout")
    public String loginOut(HttpSession session) {
        session.invalidate();
        return "user/page-login";
    }


    @RequestMapping(value = "/setting", method = RequestMethod.GET)
    public ModelAndView userSetting(HttpSession session) {
        Object email = session.getAttribute("email");
        if (email != null) {
            ModelAndView modelAndView = new ModelAndView("user/page-usersetting");
            SystemUser user = userService.getUserByEmail((String) email);
            modelAndView.addObject("user", user);
            return modelAndView;
        }

        return new ModelAndView("redirect:user/login");
    }

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public ModelAndView updateUser(SystemUser user) {
        userService.updateUser(user);
        SystemUser userByEmail = userService.getUserByEmail(user.getEmail());
        ModelAndView modelAndView = new ModelAndView("user/page-usersetting");
        modelAndView.addObject("user", userByEmail);
        return modelAndView;
    }

    @RequestMapping(value = "/admin/user", method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String, Object> removeUser(@RequestParam("id") Integer id, HttpSession session) {
        if (id != null) {
            userService.deleteUserById(id);
            //session.invalidate();
            return new JsonResponseMessage(ResponseMsg.SUCCESS).returned();
        }

        return new JsonResponseMessage(ResponseMsg.PARAMETER_ERROR).returned();

    }

    @RequestMapping(value = "/reset", method = RequestMethod.POST)
    public ModelAndView resetPwd(SystemUser user, @RequestParam("new_pwd") String newPwd) {
        ModelAndView mav = new ModelAndView("user/page-usersetting");
        if (user.getEmail() != null && user.getPassword().equals(newPwd)) {
            mav.addObject("message", "新密码不能和上一个密码相同");
            return mav;
        }
        user.setPassword(newPwd);
        userService.updateUser(user);
        mav.addObject("message", "修改密码成功");
        mav.addObject("user", user);
        return mav;
    }


    @GetMapping("/admin")
    public ModelAndView userManager() {
        ModelAndView mav = new ModelAndView("admin/page-userlist");
        List<SystemUser> users = userService.getUsers();
        mav.addObject("users", users);
        return mav;
    }

}
